using HKB.Common;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
namespace HKB.Web
{
	public class Template : Page
	{
		private static Conn conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		private static XmlDocument LabelInfor = null;
		private static string InstallDir = AppSetings.Get("InstallDir");
		private static DataRow config = new Config().config;
		public Template()
		{
			Template.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public Template(Conn _conn)
		{
			Template.conn = _conn;
		}
		public static string GetTemplatePath(string filenametype)
		{
			string text = Template.config["TemplateDir"].ToString() + Template.config[filenametype].ToString();
			return text.Replace("/", "\\");
		}
		public static string ReplaceSysLabel(string LabelContent)
		{
			SysLabel sysLabel = new SysLabel(Template.conn);
			if (LabelContent.Contains("{HL.Config.SiteName}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.SiteName}", sysLabel.ShowSiteName());
			}
			if (LabelContent.Contains("{HL.Config.Logo}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.Logo}", sysLabel.ShowLogo());
			}
			if (LabelContent.Contains("{HL.Config.InstallDir}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.InstallDir}", sysLabel.ShowInstallDir());
			}
			if (LabelContent.Contains("{HL.Config.Banner}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.Banner}", sysLabel.ShowBanner());
			}
			if (LabelContent.Contains("{HL.Config.WebMaster}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.WebMaster}", sysLabel.ShowWebMaster());
			}
			if (LabelContent.Contains("{HL.Config.WebMasterEmail}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.WebMasterEmail}", sysLabel.ShowWebMasterEmail());
			}
			if (LabelContent.Contains("{HL.Config.SiteUrl}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.SiteUrl}", sysLabel.ShowSiteUrl());
			}
			if (LabelContent.Contains("{HL.Config.PageTitle}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.PageTitle}", sysLabel.ShowPageTitle());
			}
			if (LabelContent.Contains("{HL.Config.UserCookies}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.UserCookies}", sysLabel.ShowUserCookie());
			}
			if (LabelContent.Contains("{HL.Config.LogingUserName}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.LogingUserName}", sysLabel.ShowLogingName());
			}
			if (LabelContent.Contains("{HL.Config.LogingUserID}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.LogingUserID}", sysLabel.ShowLogingUserID());
			}
			if (LabelContent.Contains("{HL.Config.TemplateDir}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.TemplateDir}", sysLabel.GetTemplateDir());
			}
			if (LabelContent.Contains("{HL.Config.SkinDir}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.SkinDir}", sysLabel.ProjectSkinDir());
			}
			if (LabelContent.Contains("{HL.Config.IndexCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.IndexCss}", sysLabel.GetDefaultIndexCss("Index"));
			}
			if (LabelContent.Contains("{HL.Config.ShopCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.ShopCss}", sysLabel.GetDefaultIndexCss("Shop"));
			}
			if (LabelContent.Contains("{HL.Config.SpecialCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.SpecialCss}", sysLabel.GetDefaultIndexCss("Special"));
			}
			if (LabelContent.Contains("{HL.Config.ProductCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.ProductCss}", sysLabel.GetDefaultIndexCss("Product"));
			}
			if (LabelContent.Contains("{HL.Config.ClassCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.ClassCss}", sysLabel.GetDefaultIndexCss("Class"));
			}
			if (LabelContent.Contains("{HL.Config.BasicCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.BasicCss}", sysLabel.GetDefaultIndexCss("Basic"));
			}
			if (LabelContent.Contains("{HL.Config.MemberCss}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.MemberCss}", sysLabel.GetDefaultIndexCss("Member"));
			}
			if (LabelContent.Contains("{HL.Config.CopyRight}"))
			{
				LabelContent = LabelContent.Replace("{HL.Config.CopyRight}", sysLabel.GetCopyRight());
			}
			if (LabelContent.Contains("{Meta_Keywords}"))
			{
				LabelContent = LabelContent.Replace("{Meta_Keywords}", sysLabel.GetMetaKeyword());
			}
			if (LabelContent.Contains("{Meta_Description}"))
			{
				LabelContent = LabelContent.Replace("{Meta_Description}", sysLabel.GetMetaDescription());
			}
			if (LabelContent.Contains("{HL.OtherTitle}"))
			{
				LabelContent = LabelContent.Replace("{HL.OtherTitle}", sysLabel.GetOtherTitle());
			}
			return LabelContent;
		}
		public static string ReplaceLanguageLabel(string LabelContent)
		{
			string text = LabelContent;
			string pattern = "{HL.Language.(.*?)}";
			MatchCollection matchCollection = Regex.Matches(LabelContent, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			if (matchCollection.Count > 0)
			{
				foreach (Match match in matchCollection)
				{
					string text2 = match.Value.Replace("{HL.Language.", "").Replace("}", "");
					text2 = text2.Replace(".", "/");
					string newValue = Language.Get(text2);
					text = text.Replace(match.Value, newValue);
				}
			}
			return text;
		}
		public static string ReplaceLabel(string LabelContent, string LabelName, bool CreateStatic)
		{
			string text = LabelContent;
			string pattern = "{" + LabelName + "(.*?)}";
			MatchCollection matchCollection = Regex.Matches(LabelContent, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			string result;
			if (matchCollection.Count > 0)
			{
				foreach (Match match in matchCollection)
				{
					string contentFormLabel = Template.GetContentFormLabel(match.Value, "", "", CreateStatic);
					text = text.Replace(match.Value, contentFormLabel);
				}
				result = Template.ReplaceLabel(text, LabelName, CreateStatic);
			}
			else
			{
				result = LabelContent;
			}
			return result;
		}
		public static string GetTemplate(string TemplatePath)
		{
			string text = HttpContext.Current.Server.MapPath("~/");
			text += TemplatePath;
			Encoding encoding = Encoding.GetEncoding(0);
			string text2;
			try
			{
				StreamReader streamReader = new StreamReader(text, encoding);
				text2 = streamReader.ReadToEnd();
				streamReader.Close();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message.ToString());
			}
			text2 = Template.RepaceRequest(text2);
			return text2;
		}
		public static string ReplacePageLabel(string LabelContent)
		{
			string text = LabelContent;
			string pattern = "{HL.ShowPageLabel(.*?)}";
			MatchCollection matchCollection = Regex.Matches(LabelContent, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			string result;
			if (matchCollection.Count > 0)
			{
				foreach (Match match in matchCollection)
				{
					string pageLabelContent = Template.GetPageLabelContent(match.Value);
					text = text.Replace(match.Value, pageLabelContent);
				}
				result = Template.ReplacePageLabel(text);
			}
			else
			{
				result = LabelContent;
			}
			return result;
		}
		public static string GetPageLabelContent(string PageLabelName)
		{
			string result;
			if (PageLabelName == null || PageLabelName == "")
			{
				result = null;
			}
			else
			{
				string labelID = Template.GetLabelID(PageLabelName);
				PageLabelBll pageLabelBll = new PageLabelBll();
				DataTable pageLabelByName = pageLabelBll.GetPageLabelByName(labelID);
				if (pageLabelByName.Rows.Count > 0)
				{
					string text = pageLabelByName.Rows[0]["Template"].ToString();
					string labelParameter = Template.GetLabelParameter(PageLabelName, "LabelID", "");
					if (Template.LabelInfor != null)
					{
						XmlNode xmlNode = Template.LabelInfor.SelectSingleNode("/Root/" + labelParameter);
						if (xmlNode == null)
						{
							result = "";
						}
						else
						{
							XmlNode xmlNode2 = xmlNode.SelectSingleNode("ItemNum");
							XmlNode xmlNode3 = xmlNode.SelectSingleNode("MaxPerPage");
							string innerXml = xmlNode2.InnerXml;
							string innerXml2 = xmlNode3.InnerXml;
							int num;
							try
							{
								num = int.Parse(HttpContext.Current.Request["page"]);
							}
							catch
							{
								num = 1;
							}
							if (num == 0)
							{
								num = 1;
							}
							int num2;
							try
							{
								num2 = int.Parse(HttpContext.Current.Request["maxperpage"]);
							}
							catch
							{
								num2 = 0;
							}
							if (num2 == 0)
							{
								num2 = int.Parse(innerXml2);
							}
							double a = Convert.ToDouble(innerXml) / (double)num2;
							int num3 = (int)Math.Ceiling(a);
							if (num > num3)
							{
								num = num3;
							}
							int num4 = num - 1;
							int num5 = num + 1;
							string text2 = HttpContext.Current.Request.RawUrl;
							if (text2.ToLower().Contains("&page=") || text2.ToLower().Contains("?page="))
							{
								int num6 = text2.ToLower().IndexOf("&page=");
								if (num6 == 0)
								{
									num6 = text2.ToLower().IndexOf("?page=");
								}
								text2 = text2.Remove(num6);
							}
							if (!text2.Contains("?"))
							{
								text2 += "?";
							}
							else
							{
								text2 += "&";
							}
							if (num4 <= 0)
							{
								text = text.Replace("{@LastPageID}", "0");
								text = text.Replace("{@LastPageUrl}", text2.Substring(0, text2.Length - 1));
							}
							else
							{
								text = text.Replace("{@LastPageID}", num4.ToString());
								text = text.Replace("{@LastPageUrl}", text2 + "page=" + num4.ToString());
							}
							text = text.Replace("{@FrontPageUrl}", text2.Substring(0, text2.Length - 1));
							text = text.Replace("{@PageID}", num.ToString());
							text = text.Replace("{@PageUrl}", text2 + "page=" + num.ToString());
							text = text.Replace("{@NextPageID}", num5.ToString());
							text = text.Replace("{@NextPageUrl}", text2 + "page=" + num5.ToString());
							text = text.Replace("{@FinallyPageID}", num3.ToString());
							text = text.Replace("{@FinallyPageUrl}", text2 + "page=" + num3.ToString());
							text = text.Replace("{@ItemNum}", innerXml.ToString());
							text = text.Replace("{@AllPage}", num3.ToString());
							text = text.Replace("{@MaxPerPage}", num2.ToString());
							result = text;
						}
					}
					else
					{
						result = "无信息！";
					}
				}
				else
				{
					result = null;
				}
			}
			return result;
		}
		public static string RepaceRequest(string temstr)
		{
			string pattern = "{@(.*?)}";
			MatchCollection matchCollection = Regex.Matches(temstr, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			string result;
			if (matchCollection.Count > 0)
			{
				for (int i = 0; i < matchCollection.Count; i++)
				{
					int length = matchCollection[i].Value.Length - 3;
					string key = matchCollection[i].Value.Substring(2, length).Trim();
					string text = HttpContext.Current.Request[key];
					if (text == null)
					{
						text = "";
					}
					else
					{
						try
						{
							int.Parse(text);
						}
						catch
						{
							text = "";
						}
					}
					temstr = temstr.Replace(matchCollection[i].Value, text.ToString().Trim());
				}
				result = temstr;
			}
			else
			{
				result = temstr;
			}
			return result;
		}
		public static string GetLabelID(string LabelText)
		{
			string pattern = "\\ ID=(.*?)(\\s|})";
			MatchCollection matchCollection = Regex.Matches(LabelText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			string result;
			if (matchCollection.Count > 0)
			{
				int length = matchCollection[0].Value.Length - 5;
				result = matchCollection[0].Value.Substring(5, length).Replace("}", "").Replace("\"", "").Trim();
			}
			else
			{
				result = null;
			}
			return result;
		}
		public static string GetLabelParameter(string LabelText, string ParameterName, string DefaultValue)
		{
			string pattern = "\\ " + ParameterName + "=(.*?)\\s";
			MatchCollection matchCollection = Regex.Matches(LabelText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			string result;
			if (matchCollection.Count > 0)
			{
				int num = ParameterName.Length + 3;
				int length = matchCollection[0].Value.Length - num;
				string text = matchCollection[0].Value.Substring(num, length).Replace("\"", "").Trim();
				if (text != null && text != "")
				{
					result = text;
				}
				else
				{
					result = DefaultValue;
				}
			}
			else
			{
				result = DefaultValue;
			}
			return result;
		}
		public static string GetContentFormLabel(string LabelText, string Define, string template, bool CreateStatic)
		{
			string text = "";
			string text2 = "";
			string a = "";
			string conntype = "";
			string text3 = "";
			string text4 = "";
			string labelID = Template.GetLabelID(LabelText);
			string result;
			if (labelID == null || labelID == "")
			{
				result = LabelText;
			}
			else
			{
				if (Define == "" || Define == null)
				{
					LabelBll labelBll = new LabelBll(Template.conn);
					DataTable labelByLabelName = labelBll.GetLabelByLabelName(labelID);
					if (labelByLabelName.Rows.Count <= 0)
					{
						result = "找不到标签：" + LabelText.Replace("{", "{#");
						return result;
					}
					text = labelByLabelName.Rows[0]["Define"].ToString();
					text2 = labelByLabelName.Rows[0]["Template"].ToString();
					string text5 = labelByLabelName.Rows[0]["Type"].ToString();
					text3 = labelByLabelName.Rows[0]["OutPutType"].ToString();
					text4 = labelByLabelName.Rows[0]["StaticOutPut"].ToString();
					if (text == null || text == "" || text2 == null || text2 == "")
					{
						result = null;
						return result;
					}
				}
				else
				{
					if (Define != "" && Define != null)
					{
						text = Define;
					}
				}
				if (CreateStatic && !string.IsNullOrEmpty(text3) && text3 == "static" && !string.IsNullOrEmpty(text4) && text4 != "")
				{
					if (text4.Contains(".js"))
					{
						result = string.Concat(new string[]
						{
							"<script type=\"text/javascript\" src=\"",
							Template.InstallDir,
							"Label/LabelOutPut/",
							text4,
							"\" charset=\"gb2312\"></script>"
						});
					}
					else
					{
						FileIO fileIO = new FileIO();
						string truePath = Fetch.GetTruePath(Template.InstallDir + "Label/LabelOutPut/" + text4);
						string text6 = "";
						try
						{
							text6 = fileIO.ReadFromFile(truePath);
						}
						catch
						{
							text6 = "静态标签页" + text4 + "出错！";
						}
						result = text6;
					}
				}
				else
				{
                    if (template != "" && template != null)
					{
                        text2 = template;
					}
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.LoadXml(text);
					XmlNode documentElement = xmlDocument.DocumentElement;
					XmlNode xmlNode = documentElement.SelectSingleNode("LabelDataType");
					string innerXml = xmlNode.InnerXml;
					string a2 = "0";
					int maxpage = 0;
					string text7 = "";
					string text8 = "";
					XmlNode xmlNode2 = documentElement.SelectSingleNode("OutType");
					if (xmlNode2 == null)
					{
						result = "{这里是个用户标签(缺少必要的参数设置！OutPutType)}";
					}
					else
					{
						a = xmlNode2.InnerXml;
						if (innerXml == "static")
						{
							if (a != "programe")
							{
								result = text2;
								return result;
							}
						}
						else
						{
							if (innerXml == "sql_sysquery" || innerXml == "sql_outquery" || innerXml == "mdb")
							{
								XmlNode xmlNode3 = documentElement.SelectSingleNode("SqlStr");
								XmlNode xmlNode4 = documentElement.SelectSingleNode("ConnStr");
								XmlNode xmlNode5 = documentElement.SelectSingleNode("EnablePage");
								if (xmlNode3 == null)
								{
									result = "{这里是个用户标签(缺少必要的参数设置！SQL)}";
									return result;
								}
								if (innerXml == "sql_outquery" || innerXml == "mdb")
								{
									if (xmlNode4 == null)
									{
										result = "{这里是个用户标签(缺少必要的参数设置！Conn)}";
										return result;
									}
								}
								if (xmlNode5 != null)
								{
									a2 = xmlNode5.InnerXml;
								}
								else
								{
									a2 = "0";
								}
								text7 = xmlNode3.InnerXml;
								if (xmlNode4 != null)
								{
									text8 = xmlNode4.InnerXml;
								}
								if (text7 == null || text7 == "")
								{
									result = "{这里是个用户标签(缺少必要的参数设置！SqlText)}";
									return result;
								}
								if (innerXml == "sql_outquery" || innerXml == "mdb")
								{
									if (text8 == null || text8 == "")
									{
										result = "{这里是个用户标签(缺少必要的参数设置！ConnStr)}";
										return result;
									}
								}
								string text9 = innerXml;
								if (text9 != null)
								{
									if (!(text9 == "mysql"))
									{
										if (!(text9 == "sql_sysquery"))
										{
											if (!(text9 == "sql_outquery"))
											{
												if (!(text9 == "mdb"))
												{
													if (text9 == "xml")
													{
														conntype = "XML";
													}
												}
												else
												{
													conntype = "Access";
												}
											}
											else
											{
												conntype = "MsSql";
											}
										}
										else
										{
											conntype = "MsSql";
											text8 = new Conn().ConnStr;
										}
									}
									else
									{
										conntype = "MySQL";
									}
								}
							}
						}
						XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/parms");
						XsltArgumentList xsltArgumentList = new XsltArgumentList();
						if (xmlNodeList != null)
						{
							foreach (XmlNode xmlNode6 in xmlNodeList)
							{
								XmlNode xmlNode7 = xmlNode6.SelectSingleNode("name");
								XmlNode xmlNode8 = xmlNode6.SelectSingleNode("default");
								if (xmlNode7 == null || xmlNode8 == null)
								{
									result = "{这里是个用户标签(缺少必要的参数设置！parms|name|default)}";
									return result;
								}
								string labelParameter = Template.GetLabelParameter(LabelText, xmlNode7.InnerXml, xmlNode8.InnerXml);
								if (innerXml == "sql_sysquery" || innerXml == "sql_outquery" || innerXml == "mdb")
								{
									if (text7 != "" && text7 != null)
									{
										text7 = text7.Replace("@" + xmlNode7.InnerXml, labelParameter);
									}
								}
								xsltArgumentList.AddParam(xmlNode7.InnerXml, "", labelParameter);
							}
						}
						if (a == "programe")
						{
							result = Template.TransXmlToHtml("", xsltArgumentList, text2);
						}
						else
						{
							if (a == "data_programe")
							{
								if (a2 == "1")
								{
									try
									{
										maxpage = int.Parse(Template.GetLabelParameter(LabelText, "MaxPerPage", "10"));
									}
									catch
									{
										maxpage = 10;
									}
								}
								string dataToXML = Template.GetDataToXML(conntype, text8, text7, maxpage, labelID);
								text2 = Template.TransXmlToHtml(dataToXML, xsltArgumentList, text2);
								result = text2;
							}
							else
							{
								if (a == "text")
								{
									string dataToString = Template.GetDataToString(conntype, text8, text7, "Str");
									result = dataToString;
								}
								else
								{
									if (a == "xml")
									{
										string dataToString = Template.GetDataToString(conntype, text8, text7, "Xml");
										result = dataToString;
									}
									else
									{
										result = null;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}
		public static string GetNodesByXml(string XmlStr, string NodesStr)
		{
			string result = "";
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlStr);
			XmlNode documentElement = xmlDocument.DocumentElement;
			XmlNode xmlNode = documentElement.SelectSingleNode(NodesStr);
			if (xmlNode != null)
			{
				result = xmlNode.InnerXml;
			}
			return result;
		}
		public static string TransXmlToHtml(string xmlreader, XsltArgumentList arguments, string xslstr)
		{
			string result;
			if (xslstr == null || xslstr == "")
			{
				result = "标签处理出错！原因：标签内容为空！";
			}
			else
			{
				if (xmlreader == "" || xmlreader == null)
				{
					xmlreader = "<?xml version=\"1.0\" encoding=\"GB2312\"?>\n<NewDataSet>\n</NewDataSet>";
				}
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(xmlreader);
				if (xmlreader.Contains("<XMLErr>"))
				{
					XmlNode xmlNode = xmlDocument.SelectSingleNode("/NewDataSet/Table/XMLErr");
					string text = xmlNode.InnerXml.Replace("<![CDATA[", "");
					text = text.Replace("]]>", "");
					result = text;
				}
				else
				{
					XmlDocument xmlDocument2 = new XmlDocument();
					try
					{
						xslstr = Template.ReplaceSysLabel(xslstr);
						xmlDocument2.LoadXml(xslstr);
					}
					catch (Exception ex)
					{
						result = "{标签处理出错！原因：(文本格式错误！预期XML格式文本)}<br/>" + ex.Message;
						return result;
					}
					XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
					xslCompiledTransform.Load(xmlDocument2);
					Stream stream = new MemoryStream();
					try
					{
						xslCompiledTransform.Transform(xmlDocument, arguments, stream);
						stream.Flush();
						stream.Position = 0L;
						StreamReader streamReader = new StreamReader(stream);
						string text2 = streamReader.ReadToEnd();
						streamReader.Close();
						stream.Close();
						result = text2;
					}
					catch (Exception ex2)
					{
						result = "{标签处理出错！原因：<br/>" + ex2.Message;
					}
				}
			}
			return result;
		}
		protected static DataTable GetDataTableFromSql(string conntype, string connstr, string sqlstr)
		{
			DataTable result;
			if (conntype == "" || sqlstr == null)
			{
				result = null;
			}
			else
			{
				DataSet dataSet = new DataSet();
				DataTable dataTable = new DataTable();
				try
				{
					if (conntype != null)
					{
						if (!(conntype == "Access"))
						{
							if (!(conntype == "MsSql"))
							{
								if (!(conntype == "MySql"))
								{
									if (conntype == "XML")
									{
										connstr = HttpContext.Current.Server.MapPath(connstr);
										dataSet.ReadXml(connstr);
										dataTable = dataSet.Tables[0];
									}
								}
								else
								{
									MySqlConnection mySqlConnection = new MySqlConnection(connstr);
									mySqlConnection.Open();
									MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
									mySqlDataAdapter.SelectCommand = new MySqlCommand(sqlstr, mySqlConnection);
									mySqlDataAdapter.Fill(dataSet, "table1");
									dataTable = dataSet.Tables["table1"];
									mySqlConnection.Close();
									mySqlDataAdapter.Dispose();
								}
							}
							else
							{
								SqlConnection sqlConnection = new SqlConnection(connstr);
								sqlConnection.Open();
								SqlCommand selectCommand = new SqlCommand(sqlstr, sqlConnection);
								SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
								sqlDataAdapter.Fill(dataSet, "table1");
								dataTable = dataSet.Tables["table1"];
								sqlConnection.Close();
								sqlConnection.Dispose();
							}
						}
						else
						{
							OleDbConnection oleDbConnection = new OleDbConnection(connstr);
							oleDbConnection.Open();
							OleDbCommand selectCommand2 = new OleDbCommand(sqlstr, oleDbConnection);
							OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(selectCommand2);
							oleDbDataAdapter.Fill(dataSet, "table1");
							dataTable = dataSet.Tables["table1"];
							oleDbDataAdapter.Dispose();
							oleDbConnection.Close();
							oleDbConnection.Dispose();
						}
					}
				}
				catch (Exception ex)
				{
					dataTable.Columns.Add("XMLErr");
					DataRow dataRow = dataTable.NewRow();
					dataRow[0] = "标签读取出错！原因是：" + ex.Message;
					dataTable.Rows.Add(dataRow);
				}
				result = dataTable;
			}
			return result;
		}
		public static string GetDataToXML(string conntype, string connstr, string sqlstr, int maxpage, string m_LabelID)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n<NewDataSet>\n");
			DataTable dataTableFromSql = Template.GetDataTableFromSql(conntype, connstr, sqlstr);
			if (dataTableFromSql != null && dataTableFromSql.Rows.Count > 0)
			{
				int num = 0;
				int num2 = dataTableFromSql.Rows.Count;
				int num3 = 0;
				int num4 = 0;
				if (maxpage > 0)
				{
					string s = HttpContext.Current.Request["page"];
					string s2 = HttpContext.Current.Request["maxperpage"];
					try
					{
						num3 = int.Parse(s);
					}
					catch
					{
						num3 = 1;
					}
					try
					{
						num4 = int.Parse(s2);
					}
					catch
					{
						num4 = maxpage;
					}
					if (num4 <= 0)
					{
						num4 = maxpage;
					}
					num = (num3 - 1) * num4;
					if (num >= dataTableFromSql.Rows.Count)
					{
						num = dataTableFromSql.Rows.Count;
					}
					num2 = num + num4;
					if (num2 > dataTableFromSql.Rows.Count)
					{
						num2 = dataTableFromSql.Rows.Count;
					}
					Template.AddLabelInforXML(m_LabelID, dataTableFromSql.Rows.Count.ToString(), num4.ToString());
				}
				for (int i = num; i < num2; i++)
				{
					stringBuilder.Append("<Table>\n");
					for (int j = 0; j < dataTableFromSql.Columns.Count; j++)
					{
						stringBuilder.Append(string.Concat(new string[]
						{
							"<",
							dataTableFromSql.Columns[j].ColumnName,
							"><![CDATA[",
							HttpContext.Current.Server.HtmlDecode(dataTableFromSql.Rows[i][j].ToString()),
							"]]></",
							dataTableFromSql.Columns[j].ColumnName,
							">\n"
						}));
					}
					stringBuilder.Append("</Table>\n");
				}
			}
			else
			{
				stringBuilder.Append("<Table></Table>\n");
			}
			stringBuilder.Append("</NewDataSet>");
			return stringBuilder.ToString();
		}
		public static string GetDataToString(string conntype, string connstr, string sqlstr, string returntype)
		{
			string result = "";
			if (!string.IsNullOrEmpty(conntype) && !string.IsNullOrEmpty(connstr) && !string.IsNullOrEmpty(sqlstr))
			{
				DataTable dataTableFromSql = Template.GetDataTableFromSql(conntype, connstr, sqlstr);
				if (dataTableFromSql != null && dataTableFromSql.Rows.Count > 0)
				{
					result = dataTableFromSql.Rows[0][0].ToString();
				}
			}
			return result;
		}
		public static void AddLabelInforXML(string ItemName, string ItemNum, string maxperpage)
		{
			XmlNode documentElement = Template.LabelInfor.DocumentElement;
			if (documentElement == null)
			{
				Template.LabelInfor.AppendChild(Template.LabelInfor.CreateXmlDeclaration("1.0", "GB2312", "yes"));
				Template.LabelInfor.AppendChild(Template.LabelInfor.CreateElement("Root"));
				documentElement = Template.LabelInfor.DocumentElement;
			}
			XmlElement xmlElement = Template.LabelInfor.CreateElement(ItemName);
			XmlElement xmlElement2 = Template.LabelInfor.CreateElement("ItemNum");
			XmlElement xmlElement3 = Template.LabelInfor.CreateElement("MaxPerPage");
			xmlElement2.InnerXml = ItemNum;
			xmlElement3.InnerXml = maxperpage;
			xmlElement.AppendChild(xmlElement2);
			xmlElement.AppendChild(xmlElement3);
			documentElement.AppendChild(xmlElement);
		}
		public static string GetControlLabelFromContent(string TemplateContent)
		{
			string pattern = "{\\$(.*?)/}";
			string text = "";
			MatchCollection matchCollection = Regex.Matches(TemplateContent, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
			if (matchCollection.Count > 0)
			{
				foreach (Match match in matchCollection)
				{
					text = text + match.Value.Substring(2, match.Value.Length - 4) + "|";
				}
				text = text.Remove(text.Length - 1);
			}
			return text;
		}
		public static void BindTemplateToPage(Page SourcePage, string PageName)
		{
			string templatePath = Template.GetTemplatePath(PageName);
			if (templatePath != null && templatePath != "")
			{
				string template = Template.GetTemplate(templatePath);
				Template.BindTextToPage(SourcePage, template);
				return;
			}
			throw new Exception(Language.Get("Shop/Template/PathErr"));
		}
		public static void BindTextToPage(Page SourcePage, string TemplateContent)
		{
			if (!string.IsNullOrEmpty(TemplateContent) && TemplateContent != "")
			{
				TemplateContent = Template.TransferLabelToHtml(TemplateContent, true);
				string controlLabelFromContent = Template.GetControlLabelFromContent(TemplateContent);
				TemplateContent = Template.ReplaceControlLabel(TemplateContent, controlLabelFromContent, SourcePage);
				Control child = new Control();
				try
				{
					child = SourcePage.ParseControl(TemplateContent);
				}
				catch (Exception ex)
				{
					throw ex;
				}
				SourcePage.Controls.Add(child);
				Template.BindLabelControl(SourcePage, controlLabelFromContent);
				return;
			}
			throw new Exception(Language.Get("Shop/Template/ContentEmpty"));
		}
		public static string ReplaceControlLabel(string LabelContent, string LabelNameList, Page SourcePage)
		{
			string[] array = LabelNameList.Split(new char[]
			{
				'|'
			});
			for (int i = 0; i < array.Length; i++)
			{
				LabelContent = LabelContent.Replace("{$" + array[i] + "/}", "<span id=\"HLProvisional_" + array[i] + "\" runat=\"server\" ></span>");
			}
			return LabelContent;
		}
		public override void VerifyRenderingInServerForm(Control control)
		{
		}
		public static void BindLabelControl(Page SourcePage, string LabelNameList)
		{
			string[] array = LabelNameList.Split(new char[]
			{
				'|'
			});
			for (int i = 0; i < array.Length; i++)
			{
				Control control = SourcePage.FindControl("HLProvisional_" + array[i]);
				if (control != null)
				{
					if (array[i] != null && array[i] != "")
					{
						Control control2 = SourcePage.FindControl(array[i]);
						if (control2 != null)
						{
							Control parent = control.Parent;
							int index = parent.Controls.IndexOf(control);
							parent.Controls.AddAt(index, control2);
							parent.Controls.Remove(control);
						}
						else
						{
							Label label = new Label();
							label.Text = "{$" + array[i] + "/}";
							control.Controls.Add(label);
						}
					}
				}
			}
		}
		public static string TransferLabelToHtml(string TemplateContent, bool CreateStatic)
		{
			TemplateContent = Template.ReplaceLabel(TemplateContent, "HL.Label", CreateStatic);
			TemplateContent = Template.ReplacePageLabel(TemplateContent);
			TemplateContent = Template.ReplaceSysLabel(TemplateContent);
			TemplateContent = Template.ReplaceLanguageLabel(TemplateContent);
			return TemplateContent;
		}
	}
}
